package com.huli.consumer;


import com.huli.service.OrdersService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = "ordersTime", consumerGroup = "ordersGroup")
public class OrdersTimeoutConsumer implements RocketMQListener<String> {

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Autowired
    OrdersService ordersService;

    @Override
    public void onMessage(String key) {
        Long expire = redisTemplate.opsForValue().getOperations().getExpire(key);
        //如果存在该值 (第10分钟查询的15分钟过期时间的键) 时间到期 执行取消订单
        if (expire != -2) {
            int id = Integer.parseInt(key.replace("orders:timeout:", ""));
            ordersService.delOrders(id);
            System.out.println("|---->订单" + id + "超时");
        }
    }
}
